package com.maiworld.content.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maiworld.content.service.ArticleService;
import com.maiworld.mapper.CategoryMapper;
import com.maiworld.mapper.NewsMapper;
import com.maiworld.pojo.Category;
import com.maiworld.pojo.News;

import entity.PageResult;


@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private CategoryMapper categoryMapper;
	@Autowired
	private NewsMapper newsMapper;
	@Override
	public List<Category> findCategotyByPId(Long id) {
		return categoryMapper.findCategotyByPid(id);
	}
	@Override
	public int addArticle(News news) {
		// TODO Auto-generated method stub
		if(news.getClassification().equals("31")) {
			List<News> newsList = newsMapper.selectByClassId(Long.parseLong(news.getClassification()), news.getStatus());
			if(newsList.size() > 0) {
				news.setId(newsList.get(0).getId());
				return newsMapper.updateNews(news);
			}else {
				return newsMapper.insert(news);
			}
		}
		return newsMapper.insert(news);
	}
	@Override
	public int deleteByIdArray(Long[] idArray) {
		// TODO Auto-generated method stub
		return newsMapper.deleteByIdArray(idArray);
	}
	@Override
	public PageResult selectByClassId(Long classId,int page, int rows,String status) {
		PageHelper.startPage(page, rows);
		// TODO Auto-generated method stub
		Page<News> pageList = (Page<News>)newsMapper.selectByClassId(classId, status);
		return new PageResult(pageList.getTotal(), pageList.getResult());
	}
	@Override
	public int saveArticle(News news) {
		// TODO Auto-generated method stub
		return newsMapper.updateNews(news);
	}
	@Override
	public List<News> selectByClassIdLimit(Long classId, String status, int start, int pageSize) {
		// TODO Auto-generated method stub
		return newsMapper.selectByClassIdAndLimit(classId, status, start, pageSize);
	}
	@Override
	public News selectById(Long id, String status) {
		// TODO Auto-generated method stub
		News news = newsMapper.selectArticleById(id, status);
		return news;
	}

	
	
}
